package xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class XML {
    public static void main(String[] args) {
        //readXML();
       // readName();
        //searchByNo("3");
        //addNewSubject("4","Mysql数据库",12,"**","mysql日常操作");
        //modify("4","***","mysql日常操作");
        delByName("Mysql数据库");
    }
    public static void readXML(){
        //first.xml
        String path="E:\\WORKSPACES\\eclipse2\\java68_advance\\advance\\first.xml";
        SAXReader reader =new SAXReader();
        Document doc=null;
        try {
             doc=reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        Element root=doc.getRootElement();
        System.out.println(root.getName());
        List<Element> list=doc.selectNodes("/subjects/subject");
        for(Element e:list){
            //e.selectNodes("name").get(0);
            String sname=e.selectSingleNode("name").getText();
            Element k=(Element)e.selectSingleNode("keshi");
            String h=k.attributeValue("hour");
            int hour=Integer.parseInt(h);
            String desc=e.selectSingleNode("desc").getText().trim();
            System.out.println(sname+","+hour+","+desc);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        }
    }
    public static void readName(){
        //first.xml
        String path="E:\\WORKSPACES\\eclipse2\\java68_advance\\advance\\first.xml";
        SAXReader reader =new SAXReader();
        Document doc=null;
        try {
            doc=reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
       // Element root=doc.getRootElement();
       // System.out.println(root.getName());
        List<Element> list=doc.selectNodes("/subjects/subject/name");
        for(Element e:list){
            System.out.println(e.getText());
        }
    }

    public static void searchByNo(String no){
        String path="E:\\WORKSPACES\\eclipse2\\java68_advance\\advance\\first.xml";
        SAXReader reader =new SAXReader();
        Document doc=null;
        try {
            doc=reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        List<Element> list=doc.selectNodes("/subjects/subject[@no='"+no+"']");
        if(list.size()==0){
            System.out.println("没有找到");
            return;
        }
        Element e=list.get(0);
        String sname=e.selectSingleNode("name").getText();
        Element k=(Element)e.selectSingleNode("keshi");
        String h=k.attributeValue("hour");
        int hour=Integer.parseInt(h);
        String desc=e.selectSingleNode("desc").getText().trim();
        System.out.println(sname+","+hour+","+desc);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }
    public static void addNewSubject(String no,String name,int hour,String desc,String diff){
        String path="E:\\WORKSPACES\\eclipse2\\java68_advance\\advance\\first.xml";
        SAXReader reader =new SAXReader();
        Document doc=null;
        try {
            doc=reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        Element root=doc.getRootElement();
        Element newE=root.addElement("subject");
        newE.addAttribute("no",no);
        newE.addElement("name").setText(name);
        newE.addElement("keshi").addAttribute("hour",String.valueOf(hour));
        newE.addElement("difficulty").setText(diff);
        newE.addElement("desc").setText(desc);
        //写回xml中去
        try {
            OutputFormat format=OutputFormat.createPrettyPrint();

            XMLWriter xm=new XMLWriter(new FileOutputStream(path),format);
            xm.write(doc);
            xm.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //修改xml
    public static void modify(String no,String diff,String desc){
        String path="E:\\WORKSPACES\\eclipse2\\java68_advance\\advance\\first.xml";
        SAXReader reader =new SAXReader();
        Document doc=null;
        try {
            doc=reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        List<Element> list=doc.selectNodes("/subjects/subject[@no='"+no+"']");
        if(list.size()==0){
            System.out.println("没有找到");
            return;
        }
        Element e=list.get(0);
        e.selectSingleNode("desc").setText(desc);
        e.selectSingleNode("difficulty").setText(diff);
        //写回xml中去
        try {
            OutputFormat format=OutputFormat.createPrettyPrint();

            XMLWriter xm=new XMLWriter(new FileOutputStream(path),format);
            xm.write(doc);
            xm.close();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
    }

    public static void delByName(String name){
        String path="E:\\WORKSPACES\\eclipse2\\java68_advance\\advance\\first.xml";
        SAXReader reader =new SAXReader();
        Document doc=null;
        try {
            doc=reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        List<Element> list=doc.selectNodes("/subjects/subject[name='"+name+"']");
        if(list.size()==0){
            System.out.println("没有找到");
            return;
        }
        Element e=list.get(0);
        e.getParent().remove(e);//!!!!!!!!!!!!!!
        try {
            OutputFormat format=OutputFormat.createPrettyPrint();

            XMLWriter xm=new XMLWriter(new FileOutputStream(path),format);
            xm.write(doc);
            xm.close();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
    }
}
